﻿using System;
using System.Threading;

namespace ErrorDumper {
    public static class ExceptionHandlerExtensions {

        #region HandleExceptionSafely

        public static string HandleExceptionSafely(
            this IExceptionHandler handler, ExceptionPropertyCollection properties, Exception exception) {

            if (handler == null)
                return null;

            try {
                return handler.HandleException(properties, exception);
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, exception);
                return null;
            }
        }

        public static string HandleExceptionSafely(
            this IExceptionHandler handler, ExceptionPropertyCollection properties, string content) {

            if (handler == null)
                return null;

            try {
                return handler.HandleException(properties, content);
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, content);
                return null;
            }
        }

        public static string HandleExceptionSafely(this IExceptionHandler handler, Exception exception) {
            if (handler == null)
                return null;

            try {
                return handler.HandleException(null, exception);
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, exception);
                return null;
            }
        }

        public static string HandleExceptionSafely(
            this IExceptionHandler handler, ExceptionPropertyCollection properties) {

            if (handler == null)
                return null;

            try {
                return handler.HandleException(properties, (Exception)null);
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, null);
                return null;
            }
        }

        #endregion

        #region HandleExceptionAsync

        public static void HandleExceptionAsync(
            this IExceptionHandler handler, ExceptionPropertyCollection properties, Exception exception) {

            if (handler == null)
                return;

            try {
                var thread = new Thread(() => handler.HandleExceptionSafely(properties, exception)) {
                    IsBackground = true
                };
                thread.Start();
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, null);
            }
        }

        public static void HandleExceptionAsync(
            this IExceptionHandler handler, ExceptionPropertyCollection properties, string content) {

            if (handler == null)
                return;

            try {
                var thread = new Thread(() => handler.HandleExceptionSafely(properties, content)) {
                    IsBackground = true
                };
                thread.Start();
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, null);
            }
        }

        public static void HandleExceptionAsync(this IExceptionHandler handler, Exception exception) {
            if (handler == null)
                return;

            try {
                var thread = new Thread(() => handler.HandleExceptionSafely(exception)) {
                    IsBackground = true
                };
                thread.Start();
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, null);
            }
        }

        public static void HandleExceptionAsync(
            this IExceptionHandler handler, ExceptionPropertyCollection properties) {

            if (handler == null)
                return;

            try {
                var thread = new Thread(() => handler.HandleExceptionSafely(properties)) {
                    IsBackground = true
                };
                thread.Start();
            }
            catch (Exception internalException) {
                InternalExceptionHandler.HandleSafely(internalException, null);
            }
        }

        #endregion
    }
}
